<?php

namespace app\admin\model;

use think\Model;

class Visitlog extends Model
{
    // 表名
    protected $name = 'visitlog';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    
    // 追加属性
    protected $append = [
        'notice_time_text',
        'way_text'
    ];

    protected static function init(){

    }


    public function customer(){
        return $this->belongsTo('Customer','customer_id')->setEagerlyType(0);
    }

    public function user(){
        return $this->belongsTo('User','user_id')->setEagerlyType(0);
    }

    public function getWayList()
    {
        return ['0' => __('Way 0'),'1' => __('Way 1'),'2' => __('Way 2'),'3' => __('Way 3'),'4' => __('Way 4')];
    }


    public function getWayTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['way']) ? $data['way'] : '');
        $list = $this->getWayList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getNoticeTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['notice_time']) ? $data['notice_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setNoticeTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }


}
